#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>

/*
输入：	./write 1.txt str1 str2
解析： argc = 4
	   argv[0] = "./write"
	   argv[1] = "1.txt"
	   argv[2] = "str1"
	   argv[3] = "str2"
*/
int main(int argc,char **argv)
{
	int fd;
	if(argc!=2)
	{
		printf("usage:%s <filename>...\n",argv[0]);
		return -1;
	}
	//打开文件
	fd = open(argv[1], O_RDWR|O_CREAT|O_TRUNC,0644); 
	if(fd<0){
		printf("can not open file %s\n",argv[1]);//打印报错
		printf("errno = %d\n",errno);	//出错的数量
		printf("err:%s\n",strerror(errno));//打印错误
		//perror("open");//打印错误	
	}
	else
	{
		printf("fd = %d\n",fd);
	}

	//指定位置写入
	printf("lseek to offset 3 from file head\n");
	lseek(fd,3,SEEK_SET);
	write(fd,"123",3);

	close(fd);
	return 0;	
}


